package com.example.restatr.Atcivity.Movie.Fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.example.restatr.Atcivity.Movie.MovieCinemaInfoAt;
import com.example.restatr.Atcivity.Movie.Bean.MovieTheatreBean;
import com.example.restatr.R;
import com.example.restatr.Tools;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

public class MovieCinemaFragment extends Fragment {
    private RecyclerView rv;
    private List<MovieTheatreBean.RowsBean> rowsBeans = new ArrayList<>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_movie_cinema, container, false);
        rv = view.findViewById(R.id.rv);
        GetMovieTheatreInfo();
        return view;
    }

    private void GetMovieTheatreInfo() {
        new Thread(() -> {
            String get = Tools.Get("/prod-api/api/movie/theatre/list", "");
            MovieTheatreBean movieTheatreBean = new Gson().fromJson(get, MovieTheatreBean.class);
            rowsBeans.addAll(movieTheatreBean.getRows());
            if (rowsBeans.size() > 0) {
                if (getActivity() != null) {
                    getActivity().runOnUiThread(() -> {
                        rv.setAdapter(new TheatreAdapter());
                    });
                }
            }
        }).start();
    }

    class TheatreAdapter extends RecyclerView.Adapter<TheatreAdapter.TheatreViewHolder> {


        @NonNull
        @Override
        public TheatreViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View inflate = LayoutInflater.from(getActivity()).inflate(R.layout.wm_item, parent, false);
            TheatreViewHolder TheatreViewHolder = new TheatreViewHolder(inflate);
            return TheatreViewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull TheatreViewHolder holder, final int position) {
            Glide.with(getActivity())
                    .load(Tools.ip + rowsBeans.get(position).getCover())
                    .error(R.drawable.bj)
                    .transform(new RoundedCorners(15))
                    .into(holder.img);
            holder.name.setText(rowsBeans.get(position).getName());
            holder.address.setText(rowsBeans.get(position).getAddress());
            holder.score.setText(rowsBeans.get(position).getScore() + "分");
            holder.saleQuantity.setVisibility(View.GONE);
            holder.saleNum3hour.setVisibility(View.GONE);
            holder.introduction.setVisibility(View.GONE);
            holder.TimeDist.setText(rowsBeans.get(position).getDistance());
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getActivity(), MovieCinemaInfoAt.class);
                    intent.putExtra("date",new Gson().toJson(rowsBeans.get(position)));
                    startActivity(intent);
                }
            });
        }

        @Override
        public int getItemCount() {
            return rowsBeans.size();
        }

        class TheatreViewHolder extends RecyclerView.ViewHolder {
            ImageView img;
            TextView name;
            TextView address;
            TextView score;
            TextView saleQuantity;
            TextView saleNum3hour;
            TextView introduction;
            TextView TimeDist;

            public TheatreViewHolder(@NonNull View itemView) {
                super(itemView);
                img = itemView.findViewById(R.id.img);
                name = itemView.findViewById(R.id.name);
                address = itemView.findViewById(R.id.address);
                score = itemView.findViewById(R.id.score);
                saleQuantity = itemView.findViewById(R.id.saleQuantity);
                saleNum3hour = itemView.findViewById(R.id.saleNum3hour);
                introduction = itemView.findViewById(R.id.introduction);
                TimeDist = itemView.findViewById(R.id.Time_Dist);
            }
        }
    }

}